//
// Created by xili on 2024/8/16 18:21.
// GO BIG OR GO HOME.
//
#include "leetcode.h"

class Solution {
public:
    bool hasGroupsSizeX(vector<int> &deck) {
        unordered_map<int, int> ump;
        for (auto &item: deck) {
            ump[item]++;
        }
        // 计算所有数量的GCD
        int mgcm = ump.begin()->second;
        for (auto &[v, n]: ump) {
            mgcm = gcd(mgcm, n);
        }
        // 检查每个数量是否可以被mgcm整除
        for (auto &[v, n]: ump) {
            if (n % mgcm != 0) {
                return false;
            }
        }
        return mgcm >= 2;
    }

    int gcd(int a, int b) {
        return b == 0 ? a : gcd(b, a % b);
    }
};
